package cn.itcast.bos.web.action;

import java.util.ArrayList;
import java.util.Collections;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.common.entity.EasyUIDatagridResult;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.CourierService;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends BaseAction<Courier> {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private CourierService courierService;
	
	/**
	 * 添加快递员
	 */
	@Action(value="courierAction_add",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String addCourier() throws Exception {
		courierService.addCourier(model);
		return SUCCESS;
	}
	
	/**
	 * 修改快递员
	 */
	@Action(value="courierAction_update",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String updateCourier() throws Exception {
		courierService.updateCourier(model);
		return SUCCESS;
	}
	
	private String ids;
	public void setIds(String ids) {
		this.ids = ids;
	}

	/**
	 * 作废快递员
	 */
	@Action(value="courierAction_batchDelete",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String batchDeleteCourier() throws Exception {
		String[] idArr = ids.split(",");
		courierService.batchDeleteCourier(idArr);
		return SUCCESS;
	}
	
	/**
	 * 欢迎快递员
	 */
	@Action(value="courierAction_batchRestore",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String batchRestoreCourier() throws Exception {
		String[] idArr = ids.split(",");
		courierService.batchRestoreCourier(idArr);
		return SUCCESS;
	}
	
	/**
	 * 分页查询
	 */
	@Action(value="courierAction_page",results={@Result(name="success",type="json")})
	public String listCourierByPage() throws Exception {
		Pageable pageable = new PageRequest(page-1, rows);
		// 创建查询条件
		Specification<Courier> spec = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 定义集合，装查询条件
				ArrayList<Predicate> list = new ArrayList<>();
				// 工号
				if (StringUtils.isNotBlank(model.getCourierNum())){
					Predicate p = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
					list.add(p);
				}
				// 所属单位
				if (StringUtils.isNotBlank(model.getCompany())){
					Predicate p = cb.equal(root.get("company").as(String.class), model.getCompany());
					list.add(p);
				}
				// 快递员类型
				if (StringUtils.isNotBlank(model.getType())){
					Predicate p = cb.like(root.get("type").as(String.class), "%"+model.getType()+"%");
					list.add(p);
				}
				// 取派标准，两张表联合查询
				// 指定连接
				Join<Courier, Standard> join = root.join("standard", JoinType.INNER);
				if (model.getStandard() != null && model.getStandard().getId() != null){
					Predicate p = cb.equal(join.get("id").as(Integer.class), model.getStandard().getId());
					list.add(p);
				}
				// 构建条件的组合
				Predicate predicate = null;
				if (list != null && list.size() > 0){
					Predicate[] array = new Predicate[list.size()];
					predicate = cb.and(list.toArray(array));
				}
				
				return predicate;
			}
			
		};
		Page<Courier> courierPage = courierService.findCourierByPage(spec,pageable);
		EasyUIDatagridResult easyUIDatagridResult = new EasyUIDatagridResult(courierPage.getTotalElements(), courierPage.getContent());
		ActionContext.getContext().getValueStack().push(easyUIDatagridResult);
		return SUCCESS;
	}

}
